const app = getApp()
const {
  navBarHeight,
  statusBarHeight,
  menuButton
} = app.globalData
const api = require('../fetch/api')
const { baseURL, ip } = require('../fetch/http')
Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0
        })
      }

      // 获取分享页面的用户id参数 用于注册生成上下级关系
      const pages = getCurrentPages()
      const currentPage = pages[pages.length-1]
      let { id, scene } = currentPage.options
      if(id) {
        wx.setStorageSync('recommend', id)
      }
      if(scene) {
        wx.setStorageSync('recommend', scene)
      }

      app.authTo(()=>{
        this.newMessage()
      })
    },
  },
  lifetimes: {
    attached() {
      this.banner()
      this.goods()
    }
  },
  data: {
    baseURL: baseURL,
    ip: ip,
    statusBarHeight: statusBarHeight,
    h: navBarHeight,
    w: menuButton.width,
    right: menuButton.right,
    bottomHeight: navBarHeight - statusBarHeight,
    arr: [],
    loading: false,
    finished: false,
    page: 1,
    show: false
  },
  methods: {
    async get() {
      const res = await api.read({
        message_id: this.data.list.map(n=>n.id)
      })
      if(res.code==200) {
        this.close()
      }
    },
    open() {
      this.setData({
        show: true
      })
    },
    close() {
      this.setData({
        show: false
      })
    },
    async newMessage() {
      const res = await api.newMessage({
        limit: 99
      })
      const list = res.data
      if(list.length>0) {
        this.setData({
          list: res.data
        })
        this.open()
      }  
    },
    async banner() {
      let res = await api.banner({
        classify: 1
      })
      this.setData({
        imgs: res.data
      })
    },
    link(e) {
      const { link } = app.dataSet(e)
      if(link) {
        if(link.startsWith("http")){
          wx.navigateTo({
            url: '/pages/home/webView/index?link' + link,
          })
        }else{
          wx.navigateTo({
            url: link,
          })
        }
      }
    },
    async goods() {
      if (!this.data.loading) {
        this.setData({
          loading: true
        })
        let res = await api.goods({
          page: this.data.page,
          limit:2,
          goods_type: 1 // 首页 1 积分 2
        })
        let finished = false
        res.data.length == 0 && (finished = true)
        this.setData({
          arr: [...this.data.arr,...res.data],
          finished,
          loading: false
        })
      }
    },
    onReachBottom() {
      // let {
      //   page,
      //   finished
      // } = this.data
      // if (!finished) {
      //   this.setData({
      //     page: page + 1
      //   })
      //   this.goods()
      // }
    },
    search() {
      wx.navigateTo({
        url: '/pages/home/search/index',
      })
    },
    goodsDetail(e) {
      const { id } = app.dataSet(e)
      wx.navigateTo({
        url: '/pages/home/goodsDetail/index?id=' + id,
      })
    },
    onShareAppMessage: function (res) {
      if (!wx.getStorageSync('token')) {
        app.toast('请登录')
        return false
      }
      if (res.from === 'button') {
        // 来自页面内转发按钮
        console.log(res.target)
      }
      return {
        imageUrl: this.data.ip + '/static/wx/img/share.png',
        title: '金盾二茅',
        path: 'index/index2?id=' + wx.getStorageSync('id') 
      }
    },
  }
})